#include "CAnimation.h"
#include <fstream>
using std::ifstream;

CAnimation::CAnimation()
{

}

CAnimation::~CAnimation()
{

}

bool CAnimation::LoadAnimation(const char* szPath)
{
	bool ret = true;

	ifstream in(szPath, std::ios_base::binary);

	if(in.is_open() && in.good())
	{
		int size;

		//reading in the length of the name
		in.read((char*)&size, sizeof(size));
		//reading in the name of the animation
		char buff[128];
		in.read(buff, size);
		m_szName = buff;

		//reading in the duration of the animation
		double duration;
		in.read((char*)&duration, sizeof(double));

		m_fDuration = (float)duration;

		//reading in the number of bones
		unsigned int numBones;
		in.read((char*)&numBones, sizeof(int));

		// clearing out and resizing...
		m_vBones.clear();
		m_vBones.resize(numBones);

		//for every bone...
		for(unsigned int bone = 0; bone < numBones; ++bone)
		{
			//bones are written out in order, so the index coorisponds with the bone value. THIS MAY NOT ALWAYS BE TRUE
			TAnimBone newBone;
			newBone.unBoneIndex = bone;
			
			//reading in the length of the name of the bone
			in.read((char*)&size, sizeof(size));
			//reading in the name of the bone
			in.read(newBone.szName, size);
			
			//reading in the number of key frames for the bone
			unsigned int numFrames;
			in.read((char*)&numFrames, sizeof(numFrames));
			
			if(numFrames - 1 == 0)
			{
				newBone.vKeyFrames.resize(1);

				TKeyFrame newFrame;
				double keyTime;
				in.read((char*)&keyTime, sizeof(double));
				newFrame.fKeyTime = (float)keyTime;

				//for(unsigned int row = 0; row < 4; ++row)
				//{
				//	for(unsigned int col = 0; col < 4; ++col)
				//	{
				//		double val;
				//		in.read((char*)&val, sizeof(double));
				//		newFrame.local.m[row][col] = (float)val;
				//	}
				//}

				for(unsigned int row = 0; row < 4; ++row)
				{
					for(unsigned int col = 0; col < 4; ++col)
					{
						double val;
						in.read((char*)&val, sizeof(double));
						newFrame.world.m[row][col] = (float)val;
					}
				}

				D3DXMatrixDecompose(&newFrame.tFrame.scale, &newFrame.tFrame.rotation, &newFrame.tFrame.pos, &newFrame.world);

				newBone.vKeyFrames[0] = newFrame;
			}
			else
			{
				newBone.vKeyFrames.resize(numFrames - 1);				

				//for every frame...
				int currFrame = -1;
				for(unsigned frame = 0; frame < numFrames; ++frame)
				{
					TKeyFrame newFrame;
					double keyTime;
					in.read((char*)&keyTime, sizeof(double));
					newFrame.fKeyTime = (float)keyTime;

					//for(unsigned int row = 0; row < 4; ++row)
					//{
					//	for(unsigned int col = 0; col < 4; ++col)
					//	{
					//		double val;
					//		in.read((char*)&val, sizeof(double));
					//		newFrame.local.m[row][col] = (float)val;
					//	}
					//}

					for(unsigned int row = 0; row < 4; ++row)
					{
						for(unsigned int col = 0; col < 4; ++col)
						{
							double val;
							in.read((char*)&val, sizeof(double));
							newFrame.world.m[row][col] = (float)val;
						}
					}

					if(currFrame != -1)
					{
						D3DXMatrixDecompose(&newFrame.tFrame.scale, &newFrame.tFrame.rotation, &newFrame.tFrame.pos, &newFrame.world);
						newBone.vKeyFrames[currFrame] = newFrame;
					}

					currFrame++;
				}
			}
			
			m_vBones[bone] = newBone;
		}
	}
	else
		ret = false;

	in.close();
	in.clear();
	
	return ret;
}
